<?php


namespace App\Http\OS\Controllers;

use OpenSearch\Client\SearchClient;
use OpenSearch\Util\SearchParamsBuilder;
use OpenSearch\Client\AppClient;
use OpenSearch\Generated\Common\Pageable;
use Illuminate\Support\Facades\DB;


class NewsOSController extends Controller{


    public function select_title($title){
        $client = OSclient2();

        $aids = select_news_action(18);
        
        $searchClient = new SearchClient($client);
        // 实例化一个搜索参数类
        $params = new SearchParamsBuilder();
        //设置config子句的start值
        $params->setStart(0);
        //设置config子句的hit值
        $params->setHits(20);
        // 指定一个应用用于搜索
        $params->setAppName('jiali_smart_news');
        // 指定搜索关键词
        $aidsStr = implode("|",$aids);
        // &&filter=in(faid,\"{$aidsStr}\")
        // $params->setQuery("default:'{$title}'&&filter=status=1");
        $params->setQuery("default:'{$title}'&&filter=in(faid,\"{$aidsStr}\") AND status=1");
        // $params->setQuery("status:'{$title}'");
        // $params->setQuery("status:'{$title}'");

        

        // $params->setQuery($aidsStr);

        // 指定返回的搜索结果的格式为json
        $params->setFormat("fulljson");
        $params->addSummary(
            array('summary_field' => 'content', 'summary_len' => 100, 'summary_ellipsis' => "。。。", 'summary_snippet' => 2, 'summary_element_prefix' => '<span class="text-red-500">', 'summary_element_postfix' => '</span>')
        );
        // $params->setFetchFields(array('id','title','description','content','createdTime','updateTime','faId'));
        
        $ret = $searchClient->execute($params->build());
        // 将json类型字符串解码
        $res = json_decode($ret->result,true)['result']['items'];
        //打印调试信息
        $ret->traceInfo->tracer;
        $newres = [];

        foreach($res as $k=>$v){
                $newres[]=$v['fields'];
        }
        
        foreach($newres as $k=>$v){
            $newres[$k]['route']=DB::table("action")->where("id",$v['faid'])->first()['route'];
            // var_dump($v);

        }

        // var_dump($newres);
        // die;
        return $newres;
    }



}